package cn.nange.cmfz.controller;

import cn.nange.cmfz.annotation.LogAnnotation;
import cn.nange.cmfz.entity.Banner;
import cn.nange.cmfz.service.BannerService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("banner")
public class BannerController {

    @Autowired
    private BannerService bannerService;

    @RequestMapping("add")
    @LogAnnotation(content="上传了一张轮播图",type = "上传轮播图")
    @ResponseBody
    public Map add(Banner b) {
        HashMap map = new HashMap();
        try {
            bannerService.add(b);
            map.put("isAdd", true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("showOne")
    @LogAnnotation(content="查看了一张轮播图",type = "查看某张轮播图")
    public Banner showOne(Integer id) {
        System.out.println(id);
        Banner banner = bannerService.showOne(id);
        System.out.println("banner = " + banner);
        return banner;
    }

    @ResponseBody
    @RequestMapping("update")
    @LogAnnotation(content="更新了一张轮播图",type = "更新轮播图")
    public Map update(Banner b) {
        HashMap map = new HashMap();
        try {
            bannerService.update(b);
            map.put("isUpdate", true);

        } catch (Exception e) {

            e.printStackTrace();

            map.put("isUpdate", false);
        }
        System.out.println("update:"+b);
        return map;
    }

    @ResponseBody
    @RequestMapping("delete")
    @LogAnnotation(content="删除了一张轮播图",type = "删除轮播图")
    public Map delete(int[] id) {
        HashMap map = new HashMap();
        try {
            for (int i = 0; i < id.length; i++) {
                bannerService.delete(id[i]);
                System.out.println(id[i]);
                map.put("isDelete", true);
            }

        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete", false);
        }
        return map;
    }

    @ResponseBody
    @RequestMapping("show")
    @LogAnnotation(content="查看了所有轮播图",type = "查看所有轮播图")
    public Map show(Integer page, Integer limit) {

        PageInfo<Banner> show = bannerService.showAll(page, limit);
        HashMap map = new HashMap();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", show.getTotal());
        map.put("data", show.getList());
        return map;
    }

    @ResponseBody
    @RequestMapping("upload")
    public Map upload(MultipartFile file) throws IOException {
        String s = UUID.randomUUID().toString() + "-" + file.getOriginalFilename();
//        file.transferTo(new File("D://static",s));
        file.transferTo(new File("/Users/nange/static/cmfz", s));
        HashMap map = new HashMap();
        map.put("code", 0);
        map.put("imgUrl", s);
        System.out.println("upload:" + map);
        return map;
    }

}
